package com.aorise.service.camera.impl;

import com.aorise.exception.DataValidationException;
import com.aorise.exception.ServiceException;
import com.aorise.mapper.camera.CameraGroupMapper;
import com.aorise.mapper.camera.CameraMapper;
import com.aorise.mapper.halt.HaltCameraMapper;
import com.aorise.mapper.halt.HaltMapper;
import com.aorise.mapper.migration.MigrationCameraMapper;
import com.aorise.mapper.migration.MigrationMapper;
import com.aorise.mapper.order.WorkOrderMapper;
import com.aorise.mapper.system.SysConfigMapper;
import com.aorise.mapper.system.UserMapper;
import com.aorise.model.camera.*;
import com.aorise.model.halt.HaltCameraModel;
import com.aorise.model.halt.HaltModel;
import com.aorise.model.migration.MigrationCameraModel;
import com.aorise.model.migration.MigrationModel;
import com.aorise.model.order.WorkOrderModel;
import com.aorise.model.system.SysConfigModel;
import com.aorise.model.system.User;
import com.aorise.service.camera.CameraGroupService;
import com.aorise.service.camera.CameraService;
import com.aorise.utils.*;
import com.aorise.utils.excel.ExcelUtil;
import com.aorise.utils.excel.ImportExcel;
import com.aorise.utils.json.InnerResult;
import com.aorise.utils.page.Page;
import net.sf.saxon.expr.instruct.ForEach;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

/**
 * @Author:cat
 * @Desicription: 视频设备接口服务实现类
 * @Date:Created in 2019-05-27
 * @Modified By:
 */
@Service
public class CameraServiceImpl implements CameraService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CameraMapper cameraMapper;
    @Autowired
    private MigrationMapper migrationMapper;
    @Autowired
    private MigrationCameraMapper migrationCameraMapper;
    @Autowired
    private WorkOrderMapper workOrderMapper;
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HaltMapper haltMapper;
    @Autowired
    private HaltCameraMapper haltCameraMapper;
    @Autowired
    private CameraGroupService cameraGroupService;
    @Autowired
    private CameraGroupMapper cameraGroupMapper;

    /**
     * @Author:cat
     * @Description 分页查询视频设备信息
     * @params: map 查询条件
     * @params: page 分页
     * @Date:2019-05-27
     * @Return: List<CameraModel> 视频设备信息集合
     * @Modified By:
     */
    @Override
    public List<CameraModel> getAllCameraListByPage(Map<String, Object> map, Page page) {
        List<CameraModel> cameraModels = null;

        /**查询总条数*/
        Long count = cameraMapper.getAllCameraListTotalByPage(map);
        if (count > 0) {
            page.setTotalCount(count);
            page.setTotalPage(count, page.getEveryPage());
            if (page.getCurrentPage() > 0 && page.getEveryPage() > 0) {
                /**查询分页条数*/
                cameraModels = cameraMapper.getAllCameraListByPage(map);
                for (int i = 0; i < cameraModels.size(); i++) {
                    if (cameraModels.get(i).getGroupId() != null && cameraModels.get(i).getGroupId() != 0) {
                        String groupName = cameraGroupService.getCameraGroupNameById(cameraModels.get(i).getGroupId());
                        cameraModels.get(i).setGroupName(groupName);
                    }
                }
            }
        } else {
            page.setTotalCount(0L);
        }
        return cameraModels;
    }

    /**
     * @Author:cat
     * @Description 根据ID查询视频设备详情
     * @params: id 主键ID
     * @params: flag 是否受配置限制：1是，2否
     * @Date:2019-05-27
     * @Return: CameraModel 视频设备信息
     * @Modified By:
     */
    @Override
    public CameraModel getCameraById(Integer id,Integer flag) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("state", ConstDefine.STATE_ABLE);
        CameraModel cameraModel = cameraMapper.getCameraById(map);

        /**查询所属分组信息*/
        if(cameraModel.getGroupId() != null && cameraModel.getGroupId() != 0){
            String groupName = cameraGroupService.getCameraGroupNameById(cameraModel.getGroupId());
            cameraModel.setGroupName(groupName);

            CameraGroupModel cameraGroupModel = cameraGroupService.getCameraGroupById(cameraModel.getGroupId());
            cameraModel.setCameraGroupModel(cameraGroupModel);
        }
        /**查询维修人员信息*/
        if(cameraModel.getMaintUser() != null && cameraModel.getMaintUser() != 0){
            User user = userMapper.selectByPrimaryKey(cameraModel.getMaintUser());
            cameraModel.setMaintUserName(user.getUsername());
        }

        Map<String, String> mapSysConfig = new HashMap<>();
        if(flag==ConstDefine.CAMERA_SHOW_YES) {
            /**查询展示配置*/
            Map<String, Object> mapCon = new HashMap<>();
            mapCon.put("conType", ConstDefine.SYSCONFIG_TYPE_1);
            mapCon.put("state", ConstDefine.STATE_ABLE);
            List<SysConfigModel> sysConfigModels = sysConfigMapper.getSysConfig(mapCon);

            for (SysConfigModel model : sysConfigModels) {
                mapSysConfig.put(model.getConName(), model.getConValue());
            }
        }

        if (cameraModel != null) {
            if (flag==ConstDefine.CAMERA_SHOW_NO || mapSysConfig.get("迁建记录").equals(ConstDefine.SYS_CONFIG_CHECK_YES)) {
                /**查询迁建记录*/
                Map<String, Object> mapM = new HashMap<>();
                mapM.put("cameraId", cameraModel.getId());
                mapM.put("status", ConstDefine.APPROVE_STATUS_PASS);
                mapM.put("state", ConstDefine.STATE_ABLE);
                List<MigrationModel> migrationModels = migrationMapper.getMigrationBycameraId(mapM);
                if (migrationModels.size() > 0) {
                    cameraModel.setMigrationModels(migrationModels);
                }
            }

            if (flag==ConstDefine.CAMERA_SHOW_NO || mapSysConfig.get("维修记录").equals(ConstDefine.SYS_CONFIG_CHECK_YES)) {
                /**查询维修记录*/
                Map<String, Object> mapW = new HashMap<>();
                mapW.put("cameraId", cameraModel.getId());
                mapW.put("wStatus", ConstDefine.WORK_ORDER_STATUS_FNISHI);
                mapW.put("state", ConstDefine.STATE_ABLE);
                List<WorkOrderModel> workOrderModels = workOrderMapper.getWorkOrderBycameraId(mapW);
                if (workOrderModels.size() > 0) {
                    cameraModel.setWorkOrderModels(workOrderModels);
                }
            }
        }
        return cameraModel;
    }

    /**
     * @Author:cat
     * @Description 新增视频设备信息
     * @params: cameraAddVO 视频设备信息
     * @Date:2019-05-27
     * @Return: Integer 主键
     * @Modified By:
     */
    @Override
    public Integer addCamera(CameraAddVO cameraAddVO) {
        /**查询设备编码是否存在*/
        Map<String, Object> caNumberMap = new HashMap<String, Object>();
        caNumberMap.put("caNumber", cameraAddVO.getCaNumber());
        caNumberMap.put("state", ConstDefine.STATE_ABLE);
        CameraModel caNumbercameraModel = cameraMapper.getCameraById(caNumberMap);
        if (caNumbercameraModel != null) {
            throw new ServiceException(StatusDefineMessage.GetMessage(StatusDefine.CAMERA_NUMBER_EXIST));
        }
        /**设备异常状态*/
        if(cameraAddVO.getOnline()==ConstDefine.CAMERA_OFFLINE){
            cameraAddVO.setException(ConstDefine.OFFLINE);
        }else {
            cameraAddVO.setException(ConstDefine.EXCEPTION);
        }

        /**新增设备信息*/
        Integer iRet = cameraMapper.addCamera(cameraAddVO);
        if (iRet > 0) {
            return cameraAddVO.getId();
        } else {
            return 0;
        }
    }

    /**
     * @Author:cat
     * @Description 修改视频设备信息
     * @params: cameraEditVO 视频设备信息
     * @Date:2019-05-27
     * @Return: Integer 影响行数
     * @Modified By:
     */
    @Override
    public Integer updateCamera(CameraEditVO cameraEditVO) {
        /**查询设备编码是否存在*/
        Map<String, Object> caNumberMap = new HashMap<String, Object>();
        caNumberMap.put("caNumber", cameraEditVO.getCaNumber());
        caNumberMap.put("state", ConstDefine.STATE_ABLE);
        CameraModel caNumbercameraModel = cameraMapper.getCameraById(caNumberMap);
        if (caNumbercameraModel != null) {
            if(!caNumbercameraModel.getId().equals(cameraEditVO.getId())) {
                throw new ServiceException(StatusDefineMessage.GetMessage(StatusDefine.CAMERA_NUMBER_EXIST));
            }
        }

        /**设备异常状态*/
        if(cameraEditVO.getOnline()==ConstDefine.CAMERA_OFFLINE){
            cameraEditVO.setException(ConstDefine.OFFLINE);
        }else {
            cameraEditVO.setException(ConstDefine.EXCEPTION);
        }
        cameraEditVO.setState(ConstDefine.STATE_ABLE);
        return cameraMapper.updateCamera(cameraEditVO);
    }

    /**
     * @Author:cat
     * @Description 导入视频设备信息
     * @params: file Excel文件
     * @params: createUser 创建人
     * @params: request
     * @Date:2019-05-28
     * @Return: InnerResult<String> 错误List集合
     * @Modified By:
     */
    @Override
    @Transactional
    public InnerResult<String> importCamera(MultipartFile file, Integer createUser) {
        ImportExcel excelReader = new ImportExcel();
        List<ArrayList<String>> dataList = new ArrayList<>();
        try {
            dataList = excelReader.readExcelContent(file.getInputStream());
        } catch (Exception e) {
            throw new DataValidationException("请使用正规的模板进行插入数据");
        }
        /**验证每列数据格式*/
        String errMsg = checkExcelData(dataList);

        if (!errMsg.equals("")) {
            /**数据格式有误*/
            return new InnerResult<String>(false, errMsg);
        } else {
            /**数据格式正确，开始导入*/
            int k = 0;
            for (ArrayList<String> list : dataList) {
                CameraAddVO cameraAddVO = new CameraAddVO();
                cameraAddVO.setCaName(list.get(0).toString().trim());
                cameraAddVO.setCaNumber(list.get(1).toString().trim());
                cameraAddVO.setIp(list.get(2).toString().trim());
                cameraAddVO.setMac(list.get(3).toString().trim());

                if (list.get(4).toString().trim().equals("")) {
                    cameraAddVO.setGroupId(null);
                } else {
                    /**查询所属分组ID*/
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("gName", list.get(4).toString().trim());
                    map.put("state", ConstDefine.STATE_ABLE);
                    CameraGroupModel cameraGroupModel = cameraGroupMapper.getCameraGroupByName(map);
                    cameraAddVO.setGroupId(cameraGroupModel.getId());
                }

                cameraAddVO.setCaType(EnumType.CA_TYPE.getId(list.get(5).toString().trim()));
                cameraAddVO.setBrand(EnumType.BRAND.getId(list.get(6).toString().trim()));
                cameraAddVO.setUnitType(list.get(7).toString().trim());
                cameraAddVO.setCaAccount(list.get(8).toString().trim());
                cameraAddVO.setCaPwd(list.get(9).toString().trim());
                cameraAddVO.setSafe(EnumType.SAFE.getId(list.get(10).toString().trim()));
                cameraAddVO.setOnline(EnumType.ONLINE.getId(list.get(11).toString().trim()));
                cameraAddVO.setNetIn(EnumType.NET_IN.getId(list.get(12).toString().trim()));

                if (list.get(13).toString().trim().equals("")) {
                    cameraAddVO.setInstallationTime(null);
                } else {
                    cameraAddVO.setInstallationTime(list.get(13).toString().trim());
                }

                cameraAddVO.setNetworkType(EnumType.NETWORK_TYPE.getId(list.get(14).toString().trim()));
                cameraAddVO.setOwner(list.get(15).toString().trim());
                cameraAddVO.setBelongDep(list.get(16).toString().trim());

                if (list.get(17).toString().trim().equals("")) {
                    cameraAddVO.setMaintUser(null);
                } else {
                    /**查询维护人员用户ID*/
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("username", list.get(17).toString().trim());
                    map.put("state", ConstDefine.STATE_ABLE);
                    List<User> userModels = userMapper.getUserByUserName(map);
                    cameraAddVO.setMaintUser(userModels.get(0).getId());
                }

                if (list.get(18).toString().trim().equals("")) {
                    cameraAddVO.setLongitude(null);
                } else {
                    cameraAddVO.setLongitude(list.get(18).toString().trim());
                }
                if (list.get(19).toString().trim().equals("")) {
                    cameraAddVO.setLatitude(null);
                } else {
                    cameraAddVO.setLatitude(list.get(19).toString().trim());
                }
                if (list.get(20).toString().trim().equals("")) {
                    cameraAddVO.setHeight(null);
                } else {
                    cameraAddVO.setHeight(list.get(20).toString().trim());
                }
                if (list.get(21).toString().trim().equals("")) {
                    cameraAddVO.setRadius(null);
                } else {
                    cameraAddVO.setRadius(list.get(21).toString().trim());
                }
                if (list.get(22).toString().trim().equals("")) {
                    cameraAddVO.setView(null);
                } else {
                    cameraAddVO.setView(list.get(22).toString().trim());
                }
                cameraAddVO.setOrientation(EnumType.ORIENTATION.getId(list.get(23).toString().trim()));
                cameraAddVO.setFunction(list.get(24).toString().trim());
                cameraAddVO.setUprightNumber(list.get(25).toString().trim());
                cameraAddVO.setFillLight(list.get(26).toString().trim());
                if (list.get(27).toString().trim().equals("")) {
                    cameraAddVO.setWarrantyPeriod(null);
                } else {
                    cameraAddVO.setWarrantyPeriod(list.get(27).toString().trim());
                }
                cameraAddVO.setContractor(list.get(28).toString().trim());
                cameraAddVO.setMaintainer(list.get(29).toString().trim());
                cameraAddVO.setSecurityType(EnumType.SECURITY_TYPE.getId(list.get(30).toString().trim()));
                cameraAddVO.setIndoorOutdoor(EnumType.INDOOR_OUTDOOR.getId(list.get(31).toString().trim()));
                cameraAddVO.setLocation(EnumType.LOCATION.getId(list.get(32).toString().trim()));
                cameraAddVO.setLandmarks(list.get(33).toString().trim());
                cameraAddVO.setCreateUser(createUser);

                /**设备异常状态*/
                if(cameraAddVO.getOnline()==ConstDefine.CAMERA_OFFLINE){
                    cameraAddVO.setException(ConstDefine.OFFLINE);
                }else {
                    cameraAddVO.setException(ConstDefine.EXCEPTION);
                }

                if (cameraMapper.addCamera(cameraAddVO) < 0) {
                    throw new DataValidationException("插入第" + (k + 3) + "行数据失败!");
                }
                k += 1;
            }
            return new InnerResult<String>(true, "成功导入"+ dataList.size() +"条数据!");
        }
    }

    /**
     * 验证Excel数据格式
     *
     * @param dataList
     * @return
     */
    private String checkExcelData(List<ArrayList<String>> dataList) {
        /**数据效验*/
        DataValidation dataValidation = new DataValidation();

        int k = 0;
        String errStr = "";
        String detailsErrStr = "";
        List caNumberList = new ArrayList();

        for (ArrayList<String> list : dataList) {

            if (list.size() != 34) {
                throw new DataValidationException("请使用正规的模板进行插入数据");
            }

            String msgStr = "";

            /**验证文档内容*/
            for (int i = 0; i < list.size(); i++) {
                switch (i) {
                    case 0:
                        /**设备名称*/
                        if (list.get(i).toString().trim()=="") {
                            detailsErrStr += "第" + (k + 3) + "行设备名称为必填项!";
                        }else {
                            if (!dataValidation.chkLength(list.get(i).toString().trim(), 0, 20)) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 1:
                        /**设备编码*/
                        if (list.get(i).toString().trim()=="") {
                            detailsErrStr += "第" + (k + 3) + "行设备编码为必填项!";
                        }else {
                            if (!dataValidation.chkLength(list.get(i).toString().trim(), 0, 30)) {
                                msgStr += "第" + (i + 1) + "列";
                            } else {
                                /**查询设备编码是否存在*/
                                Map<String, Object> caNumberMap = new HashMap<>();
                                caNumberMap.put("caNumber", list.get(i));
                                caNumberMap.put("state", ConstDefine.STATE_ABLE);
                                CameraModel caNumbercameraModel = cameraMapper.getCameraById(caNumberMap);
                                if (caNumbercameraModel != null) {
                                    detailsErrStr += "第" + (k + 3) + "行设备编码已存在!";
                                } else {
                                    /**保存每一行的设备编码，用来判断是否有重复*/
                                    caNumberList.add(list.get(i));
                                }
                            }
                        }
                        break;
                    case 2:
                        /**IP地址*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chekeIP4(list.get(i).toString().trim())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 3:
                        /**MAC地址*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chekeMAC(list.get(i).toString().trim())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 4:
                        /**所属分组*/
                        if (list.get(i).toString().trim()=="") {
                            detailsErrStr += "第" + (k + 3) + "行所属分组为必填项!";
                        }else {
                            /**查询所属分组是否存在*/
                            Map<String, Object> map = new HashMap<String, Object>();
                            map.put("gName", list.get(i).toString().trim());
                            map.put("state", ConstDefine.STATE_ABLE);
                            CameraGroupModel cameraGroupModel = cameraGroupMapper.getCameraGroupByName(map);
                            if (cameraGroupModel == null) {
                                detailsErrStr += "第" + (k + 3) + "行所属分组不存在!";
                            }
                        }
                        break;
                    case 5:
                        /**设备类型*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!EnumType.CA_TYPE.isHas(list.get(i).toString())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 6:
                        /**设备品牌*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!EnumType.BRAND.isHas(list.get(i).toString())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 7:
                        /**设备型号*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chkLength(list.get(i).toString().trim(), 0, 20)) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 8:
                        /**设备帐号*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chkLength(list.get(i).toString().trim(), 0, 20)) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 9:
                        /**设备口令*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chkLength(list.get(i).toString().trim(), 0, 20)) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 10:
                        /**设备安全状态*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!EnumType.SAFE.isHas(list.get(i).toString())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 11:
                        /**连接状态*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!EnumType.ONLINE.isHas(list.get(i).toString())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 12:
                        /**入网状态*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!EnumType.NET_IN.isHas(list.get(i).toString())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 13:
                        /**安装时间*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.checkDate(list.get(i).toString().trim())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 14:
                        /**接入网络*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!EnumType.NETWORK_TYPE.isHas(list.get(i).toString())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 15:
                        /**建设单位*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chkLength(list.get(i).toString().trim(), 0, 50)) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 16:
                        /**设备所属部门*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chkLength(list.get(i).toString().trim(), 0, 50)) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 17:
                        /**维护人员*/
                        if (!list.get(i).toString().trim().equals("")) {
                            /**查询维护人员是否存在*/
                            Map<String, Object> map = new HashMap<String, Object>();
                            map.put("username", list.get(i).toString().trim());
                            map.put("state", ConstDefine.STATE_ABLE);
                            List<User> userModels = userMapper.getUserByUserName(map);
                            if (userModels.size() == 0) {
                                detailsErrStr += "第" + (k + 3) + "行维护人员不存在!";
                            } else if (userModels.size() > 1) {
                                detailsErrStr += "第" + (k + 3) + "行系统存在同名维护人员!";
                            }
                        }
                        break;
                    case 18:
                        /**经度*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chekeNumber(list.get(i).toString().trim())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 19:
                        /**纬度*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chekeNumber(list.get(i).toString().trim())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 20:
                        /**高度*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chekeNumber(list.get(i).toString().trim())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 21:
                        /**可视半径(米)*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chekeNumber(list.get(i).toString().trim())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 22:
                        /**水平视角（度）*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chekeInteger(list.get(i).toString().trim())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 23:
                        /**镜头朝向*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!EnumType.ORIENTATION.isHas(list.get(i).toString())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 24:
                        /**设备功能*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chkLength(list.get(i).toString().trim(), 0, 50)) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 25:
                        /**立杆编号*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chkLength(list.get(i).toString().trim(), 0, 50)) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 26:
                        /**补光属性*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chkLength(list.get(i).toString().trim(), 0, 20)) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 27:
                        /**保修截止日期*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.checkDate(list.get(i).toString().trim())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 28:
                        /**承建单位*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chkLength(list.get(i).toString().trim(), 0, 50)) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 29:
                        /**维护单位*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chkLength(list.get(i).toString().trim(), 0, 50)) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 30:
                        /**监控类型*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!EnumType.SECURITY_TYPE.isHas(list.get(i).toString())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 31:
                        /**室内/室外*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!EnumType.INDOOR_OUTDOOR.isHas(list.get(i).toString())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 32:
                        /**位置类型*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!EnumType.LOCATION.isHas(list.get(i).toString())) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                    case 33:
                        /**周边标志*/
                        if (!list.get(i).toString().trim().equals("")) {
                            if (!dataValidation.chkLength(list.get(i).toString().trim(), 0, 50)) {
                                msgStr += "第" + (i + 1) + "列";
                            }
                        }
                        break;
                }
            }
            if (msgStr != "") {
                errStr += "第" + (k + 3) + "行," + msgStr + "数据格式有误。";
            }
            k += 1;
        }

        /**判断设备编码是否有重复*/
        if (caNumberList.size() > 0) {
            HashSet set = new HashSet<>(caNumberList);
            if (set.size() != caNumberList.size()) {
                detailsErrStr += "文件设备编码有重复!";
            }
        }
        return errStr + detailsErrStr;
    }

    /**
     * @Author:cat
     * @Description 导出视频设备信息
     * @params: map 查询条件
     * @params: response
     * @Date:2019-05-29
     * @Modified By:
     */
    @Override
    public void exportCamera(Map<String, Object> map, HttpServletResponse response) {
        List<CameraModel> cameraModels = cameraMapper.exportCamera(map);
        if (cameraModels.size() > 0) {

            List<String[]> list = new ArrayList<>();
            for (int i = 0; i < cameraModels.size(); i++) {
                String arr[] = new String[34];
                arr[0] = cameraModels.get(i).getCaName();
                arr[1] = cameraModels.get(i).getCaNumber();
                arr[2] = cameraModels.get(i).getIp();
                arr[3] = cameraModels.get(i).getMac();

                if(cameraModels.get(i).getGroupId() != null && cameraModels.get(i).getGroupId()!= 0) {
                    arr[4] = cameraGroupService.getCameraGroupNameById(cameraModels.get(i).getGroupId());
                }

                arr[5] = EnumType.CA_TYPE.getValue(cameraModels.get(i).getCaType());
                arr[6] = EnumType.BRAND.getValue(cameraModels.get(i).getBrand());
                arr[7] = cameraModels.get(i).getUnitType();
                arr[8] = cameraModels.get(i).getCaAccount();
                arr[9] = cameraModels.get(i).getCaPwd();
                arr[10] = EnumType.SAFE.getValue(cameraModels.get(i).getSafe());
                arr[11] = EnumType.ONLINE.getValue(cameraModels.get(i).getOnline());
                arr[12] = EnumType.NET_IN.getValue(cameraModels.get(i).getNetIn());
                arr[13] = cameraModels.get(i).getInstallationTime();
                arr[14] = EnumType.NETWORK_TYPE.getValue(cameraModels.get(i).getNetworkType());
                arr[15] = cameraModels.get(i).getOwner();
                arr[16] = cameraModels.get(i).getBelongDep();

                if (cameraModels.get(i).getMaintUser() != null && cameraModels.get(i).getMaintUser()!= 0) {
                    User user = userMapper.selectByPrimaryKey(cameraModels.get(i).getMaintUser());
                    arr[17] = user.getUsername();
                }
                arr[18] = cameraModels.get(i).getLongitude();
                arr[19] = cameraModels.get(i).getLatitude();
                arr[20] = cameraModels.get(i).getHeight();
                arr[21] = cameraModels.get(i).getRadius();
                if (cameraModels.get(i).getView() != null) {
                    arr[22] = cameraModels.get(i).getView().toString();
                }
                arr[23] = EnumType.ORIENTATION.getValue(cameraModels.get(i).getOrientation());
                arr[24] = cameraModels.get(i).getFunction();
                arr[25] = cameraModels.get(i).getUprightNumber();
                arr[26] = cameraModels.get(i).getFillLight();
                arr[27] = cameraModels.get(i).getWarrantyPeriod();
                arr[28] = cameraModels.get(i).getContractor();
                arr[29] = cameraModels.get(i).getMaintainer();
                arr[30] = EnumType.SECURITY_TYPE.getValue(cameraModels.get(i).getSecurityType());
                arr[31] = EnumType.INDOOR_OUTDOOR.getValue(cameraModels.get(i).getIndoorOutdoor());
                arr[32] = EnumType.LOCATION.getValue(cameraModels.get(i).getLocation());
                arr[33] = cameraModels.get(i).getLandmarks();
                list.add(arr);
            }
            /**导出Excel*/
            ExcelUtil.writeExcel(list, null,34, 3, "视频设备信息","camera_list.xlsx", response);
        }
    }

    /**
     * @Author:cat
     * @Description 下载导入视频设备信息模板
     * @params: response
     * @Date:2019-05-29
     * @Modified By:
     */
    @Override
    public void downloadCameraTemplate(HttpServletResponse response) {
        try {
            ClassPathResource classPathResource = new ClassPathResource("template/camera_list.xlsx");
            String filePath = null;
            filePath = classPathResource.getFile().getAbsolutePath();
            ExcelUtil.downloadFile(response, filePath, "camera_list", false);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * @Author:cat
     * @Description 批量修改视频设备的入网状态、异常类型、停机状态、坐标
     * @params: map 查询条件Map
     * @Date:2019-05-30
     * @Return: Integer 影响行数
     * @Modified By:
     */
    @Override
    public Integer updateCameraStatus(Map<String, Object> map) {

        return cameraMapper.updateCameraStatus(map);
    }

    /**
     * @Author:cat
     * @Description 根据筛选条件查询视频设备信息
     * @params: map 查询条件
     * @Date:2019-06-01
     * @Return: List<CameraModel> 视频设备信息集合
     * @Modified By:
     */
    @Override
    public List<CameraModel> getCameraList(Map<String, Object> map) {
        List<CameraModel> cameraModels = cameraMapper.getCameraList(map);
        for (int i = 0; i < cameraModels.size(); i++) {
            if (cameraModels.get(i).getGroupId() != null && cameraModels.get(i).getGroupId() != 0) {
                String groupName =cameraGroupService.getCameraGroupNameById(cameraModels.get(i).getGroupId());
                cameraModels.get(i).setGroupName(groupName);
            }
        }
        return cameraModels;
    }

    /**
     * @Author:cat
     * @Description 每天0点1分更新视频设备坐标、停机状态
     * @Date:2019-06-12
     * @Modified By:
     */
    @Override
    public void updateCameraAttribute() {
        /**------------------------更新视频设备坐标-----------------------*/
        try {
            /**查询昨天完成的点位迁建信息*/
            Map<String, Object> mapM = new HashMap<>();
            mapM.put("status", ConstDefine.APPROVE_STATUS_PASS);
            mapM.put("state", ConstDefine.STATE_ABLE);
            List<MigrationModel> migrationModels = migrationMapper.getFinishMigration(mapM);
            if (migrationModels.size() > 0) {
                for (int i = 0; i < migrationModels.size(); i++) {
                    /**查询迁建信息的视频设备集合*/
                    Map<String, Object> mapMC = new HashMap<>();
                    mapMC.put("migrationId", migrationModels.get(i).getId());
                    mapMC.put("state", ConstDefine.STATE_ABLE);
                    List<MigrationCameraModel> migrationCameraModels = migrationCameraMapper.getMigrationCameraByMigrationId(mapMC);

                    /**拼接要更新坐标的视频设备ID*/
                    String cameraIds = "";
                    for (int j = 0; j < migrationCameraModels.size(); j++) {
                        cameraIds += migrationCameraModels.get(j).getCameraId() + ",";
                    }
                    if (cameraIds != "") {
                        cameraIds = cameraIds.substring(0, cameraIds.length() - 1);

                        /**更新视频设备的坐标*/
                        Map<String, Object> mapC = new HashMap<>();
                        mapC.put("longitude", migrationModels.get(i).getNewLongitude());
                        mapC.put("latitude", migrationModels.get(i).getNewLatitude());
                        mapC.put("id", cameraIds);
                        mapC.put("state", ConstDefine.STATE_ABLE);
                        cameraMapper.updateCameraStatus(mapC);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("更新视频设备坐标失败");
        }

        /**------------------------更新视频设备停机状态---------------------*/
        try {
            /**查询今天开始停机的数据*/
            Map<String, Object> mapH = new HashMap<>();
            mapH.put("status", ConstDefine.APPROVE_STATUS_PASS);
            mapH.put("state", ConstDefine.STATE_ABLE);
            List<HaltModel> haltModels = haltMapper.getFinishHalt(mapH);
            if (haltModels.size() > 0) {
                for (int i = 0; i < haltModels.size(); i++) {
                    /**查询计划停机的视频设备集合*/
                    Map<String, Object> mapHC = new HashMap<>();
                    mapHC.put("haltId", haltModels.get(i).getId());
                    mapHC.put("state", ConstDefine.STATE_ABLE);
                    List<HaltCameraModel> haltCameraModels = haltCameraMapper.getHaltCameraByHaltId(mapHC);

                    /**拼接要更新停机状态的视频设备ID*/
                    String cameraIds = "";
                    for (int j = 0; j < haltCameraModels.size(); j++) {
                        cameraIds += haltCameraModels.get(j).getCameraId() + ",";
                    }
                    if (cameraIds != "") {
                        cameraIds = cameraIds.substring(0, cameraIds.length() - 1);

                        /**更新视频设备的停机状态*/
                        Map<String, Object> mapC = new HashMap<>();
                        mapC.put("halt", ConstDefine.HALT_YES);
                        mapC.put("id", cameraIds);
                        mapC.put("state", ConstDefine.STATE_ABLE);
                        cameraMapper.updateCameraStatus(mapC);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("更新视频设备停机状态失败");
        }
    }

}
